package com.dkf.springcloud.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitConfig {
    public static final String ONE_NAME_1="one_name_1";
    public static final String TWO_NAME_2="two_name_2";
    public static final String THREE="Queue3";
    public static final String EXCHANGE_1="exchange_1";
    public static final String EXCHANGE_2="exchange_2";
    //所有队列必须持久化(Queue默认durable:true)，防止mq服务重启后消息丢失
    @Bean
    public Queue oneQueue(){
        /**
         durable="true" 持久化消息队列 ， rabbitmq重启的时候不需要创建新的队列
         auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
         exclusive  表示该消息队列是否只在当前connection生效,默认是false
         */
        return new Queue(ONE_NAME_1,true,false,false);//等同于 return new Queue(ONE_NAME_1);
    }

    @Bean
    public Queue twoQueue(){
        return new Queue(TWO_NAME_2);
    }

    @Bean
    public Queue threeQueue(){
        return new Queue(THREE);
    }

    /**
     *   1.定义direct exchange，绑定first_exchange
     *   2.durable="true" 持久化交换机， rabbitmq重启的时候不需要创建新的交换机
     *   3.direct交换器相对来说比较简单，匹配规则为：如果路由键匹配，消息就被投送到相关的队列
     *     fanout交换器中没有路由键的概念，他会把消息发送到所有绑定在此交换器上面的队列中。
     *     topic交换器你采用模糊匹配路由键的原则进行转发消息到队列中
     */

    @Bean
    public DirectExchange EXCHANGE_1(){
        DirectExchange exchange=new DirectExchange(EXCHANGE_1,true,false);
        return exchange;
    }

    @Bean
    public TopicExchange EXCHANGE_2(){
        TopicExchange exchange=new TopicExchange(EXCHANGE_2,true,false);
        return exchange;
    }

    @Bean
    public Binding bind(){
        Binding binding = BindingBuilder.bind(oneQueue()).to(EXCHANGE_1()).with(ONE_NAME_1);
        return binding;
    }

    @Bean
    public Binding bind2(){
        Binding binding = BindingBuilder.bind(threeQueue()).to(EXCHANGE_2()).with("#.weather");
        return binding;
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }







}
